$(function(){
	var goodsNum = $("#J_quantity #inputNum").val();
    $("#J_quantity .sub").click(function(){//加
        goodsNum = $("#J_quantity #inputNum").val();//商品数量
        if(goodsNum<=1){
            $(this).addClass("disabled");
            if(goodsNum==1){
                $("#J_quantity #inputNum").val(goodsNum);
            }
        }else{
            $("#J_quantity .add").removeClass("disabled");
            goodsNum --;
            $("#J_quantity #inputNum").val(goodsNum);
        }

    });
	$("#J_quantity .add").click(function(){
		goodsNum = $("#J_quantity #inputNum").val();//商品数量
		if(goodsNum>=200){
			$(this).addClass("disabled");
			if(goodsNum==200){
				$("#J_quantity #inputNum").val(goodsNum);
			}
		}else{
			$("#J_quantity .sub").removeClass('disabled');
			goodsNum ++;
			$("#J_quantity #inputNum").val(goodsNum);
		}
	});
	$("#J_quantity #inputNum").blur(function(){
		var inputNum =$("#J_quantity #inputNum").val();
		if(!isNaN(inputNum)){
			if(inputNum>=200){
				inputNum = 200;
			}else if(inputNum <=1){
				inputNum = 1;
			}
			goodsNum = inputNum;
		}
		$("#J_quantity #inputNum").val(goodsNum);
	});

	$(".g_attr_con .tab_item").click(function(){
		$(this).addClass('active').siblings().removeClass("active");
	});


	// 图片上下滚动
	var count = $("#J_preview .item_img").length - 5; /* 显示 6 个 li标签内容 */
	var interval = $("#J_preview .item_img:first").width();
	var curIndex = 0;
	
	/*$('.scrollbutton').click(function(){
		if( $(this).hasClass('disabled') ) return false;
		
		if ($(this).hasClass('smallImgUp')) --curIndex;
		else ++curIndex;
		
		$('.scrollbutton').removeClass('disabled');
		if (curIndex == 0) $('.smallImgUp').addClass('disabled');
		if (curIndex == count-1) $('.smallImgDown').addClass('disabled');
		
		$("#imageMenu ul").stop(false, true).animate({"marginLeft" : -curIndex*interval + "px"}, 600);
	});	*/
	// 解决 ie6 select框 问题
	$.fn.decorateIframe = function(options) {
        if ($.browser.msie && $.browser.version < 7) {
            var opts = $.extend({}, $.fn.decorateIframe.defaults, options);
            $(this).each(function() {
                var $myThis = $(this);
                //创建一个IFRAME
                var divIframe = $("<iframe />");
                divIframe.attr("id", opts.iframeId);
                divIframe.css("position", "absolute");
                divIframe.css("display", "none");
                divIframe.css("display", "block");
                divIframe.css("z-index", opts.iframeZIndex);
                divIframe.css("border");
                divIframe.css("top", "0");
                divIframe.css("left", "0");
                if (opts.width == 0) {
                    divIframe.css("width", $myThis.width() + parseInt($myThis.css("padding")) * 2 + "px");
                }
                if (opts.height == 0) {
                    divIframe.css("height", $myThis.height() + parseInt($myThis.css("padding")) * 2 + "px");
                }
                divIframe.css("filter", "mask(color=#fff)");
                $myThis.append(divIframe);
            });
        }
    }
    $.fn.decorateIframe.defaults = {
        iframeId: "decorateIframe1",
        iframeZIndex: -1,
        width: 0,
        height: 0
    }
    //放大镜视窗
    $("#bigView").decorateIframe();
    //点击到中图
    var midChangeHandler = null;
	
    $("#J_preview .item_img img").bind("click", function(){
		midChange($(this).attr("data-simg"),$(this).attr("data-bimg"));
		$(this).parent().addClass('hover').siblings().removeClass('hover');
	}).bind("mouseover", function(){
		midChange($(this).attr("data-simg"),$(this).attr("data-bimg"));
		$(this).parent().addClass('hover').siblings().removeClass('hover');
	});
    function midChange(src,bimg) {
    	$("#J_midimg img").attr("data-bimg", bimg);
    	$("#J_midimg img").attr("src", src).load(function() {
            changeViewImg();
        });
    }
    //大视窗看图
    function mouseover(e) {
        if ($("#winSelector").css("display") == "none") {
            $("#winSelector,#bigView").show();
        }
        $("#winSelector").css(fixedPosition(e));
        e.stopPropagation();
    }
    function mouseOut(e) {
        if ($("#winSelector").css("display") != "none") {
            $("#winSelector,#bigView").hide();
        }
        e.stopPropagation();
    }
    $("#J_midimg").mouseover(mouseover); //中图事件
    $("#J_midimg,#winSelector").mousemove(mouseover).mouseout(mouseOut); //选择器事件

    var $divWidth = $("#winSelector").width(); //选择器宽度
    var $divHeight = $("#winSelector").height(); //选择器高度
    var $imgWidth = $("#J_midimg").width(); //中图宽度
    var $imgHeight = $("#J_midimg").height(); //中图高度
    var $viewImgWidth = $viewImgHeight = $height = null; //IE加载后才能得到 大图宽度 大图高度 大图视窗高度

    function changeViewImg() {
        $("#bigView img").attr("src", $("#J_midimg img").attr("data-bimg"));
    }
    changeViewImg();
    $("#bigView").scrollLeft(0).scrollTop(0);
    function fixedPosition(e) {
        if (e == null) {
            return;
        }
        var $imgLeft = $("#J_midimg").offset().left; //中图左边距
        var $imgTop = $("#J_midimg").offset().top; //中图上边距
        X = e.pageX - $imgLeft - $divWidth / 2; //selector顶点坐标 X
        Y = e.pageY - $imgTop - $divHeight / 2; //selector顶点坐标 Y
        X = X < 0 ? 0 : X;
        Y = Y < 0 ? 0 : Y;
        X = X + $divWidth > $imgWidth ? $imgWidth - $divWidth : X;
        Y = Y + $divHeight > $imgHeight ? $imgHeight - $divHeight : Y;

        if ($viewImgWidth == null) {
            $viewImgWidth = $("#bigView img").outerWidth();
            $viewImgHeight = $("#bigView img").height();
            if ($viewImgWidth < 200 || $viewImgHeight < 200) {
                $viewImgWidth = $viewImgHeight = 800;
            }
            $height = $divHeight * $viewImgHeight / $imgHeight;
            $("#bigView").width($divWidth * $viewImgWidth / $imgWidth);
            $("#bigView").height($height);
        }
        var scrollX = X * $viewImgWidth / $imgWidth;
        var scrollY = Y * $viewImgHeight / $imgHeight;
        $("#bigView img").css({ "left": scrollX * -1, "top": scrollY * -1 });
        $("#bigView").css({ "top": 0, "left": $(".preview").width() + 15 });

        return { left: X, top: Y };
    }

    /*shopcart ==*/
    $(".goods_card .btn-cart").click(function(event){
        var _img =$("#J_midimg img").attr('src'); //获取当前点击图片链接
        var flyer = $('<img class="flyer-img" src="' + _img + '" style="z-index: 99;width: 50px;height:50px;">'); //抛物体对象
        var rtoolbar_left = $(".fixed_toolbar_R .shop_cart").offset().left+10;
        var rtoolbar_top = $(".fixed_toolbar_R .shop_cart").offset().top-$(document).scrollTop();
        flyer.fly({
            start: {
                left: $(this).offset().left+20,//抛物体起点横坐标
                top: $(this).offset().top-$(document).scrollTop() //抛物体起点纵坐标
            },
            end: {
                left: rtoolbar_left,//抛物体终点横坐标
                top: rtoolbar_top+50, //抛物体终点纵坐标
                 width: 0,
                height: 0
            },
            onEnd: function() {
                flyer.remove();
            }
        });
    });
})